﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;


namespace LBCMS.BLL
{
    public class login
    {
        LBCMS.EntityFrameWork.BLLContext db = new EntityFrameWork.BLLContext();
        Helper.JsonHelper.retData<string> ret = new Helper.JsonHelper.retData<string>();
        public Helper.JsonHelper.retData<string> getSession(string userGuid)
        {
            var user = db.B_Users.FirstOrDefault(p => p.userGuid == userGuid);
            if (user != null)
            {
                FormsAuthentication.SetAuthCookie(user.userName, true);
                System.Web.HttpContext.Current.Session["userName"] = user.userName;
                System.Web.HttpContext.Current.Session["userGuid"] = user.userGuid;
                System.Web.HttpContext.Current.Session["loginID"] = user.loginID;

                ret.success = true;
                ret.msg = "登录成功！";
            }
            else
            {
                ret.success = false;
                ret.msg = "用户名或密码错误！";
            }

            return ret;
        }


        public Helper.JsonHelper.retData<string> getSession(string loginID,string password)
        {
            var enPwd = Helper.EncryptHelper.SHA1(password);
            var user = db.B_Users.FirstOrDefault(p => p.loginID == loginID && p.password == enPwd);
            if (user != null)
            {
                FormsAuthentication.SetAuthCookie(user.userName, true);
                System.Web.HttpContext.Current.Session["userName"] = user.userName;
                System.Web.HttpContext.Current.Session["userGuid"] = user.userGuid;
                System.Web.HttpContext.Current.Session["loginID"] = user.loginID;

                ret.success = true;
                ret.msg = "登录成功！";
            }
            else
            {
                ret.success = false;
                ret.msg = "用户名或密码错误！";
            }

            return ret;
        }
        public Helper.JsonHelper.retData<string> getSessionByLoginID(string loginID)
        {
            var user = db.B_Users.FirstOrDefault(p => p.loginID == loginID);
            if (user != null)
            {
                FormsAuthentication.SetAuthCookie(user.userName, true);
                System.Web.HttpContext.Current.Session["userName"] = user.userName;
                System.Web.HttpContext.Current.Session["userGuid"] = user.userGuid;
                System.Web.HttpContext.Current.Session["loginID"] = user.loginID;

                ret.success = true;
                ret.msg = "登录成功！";
            }
            else
            {
                ret.success = false;
                ret.msg = "用户名或密码错误！";
            }

            return ret;
        }
    }
}